package com.wwp.tbshop.view.fragment.category;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wwp.tbshop.model.bean.CategoryInfo;
import com.wwp.tbshop.model.bean.CategoryVo;
import com.wwp.tbshop.presenter.CategoryFragmentPresenter;
import com.wwp.tbshop.util.UiUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 楠GG on 2017/5/25.
 */

public abstract class CategoryBaseFragment extends Fragment {
    private static final String TAG = "CategoryBaseFragment";
    private HeimaAsyncTask mCommonView;
    private CategoryFragmentPresenter mCategoryFragmentPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: "+CategoryBaseFragment.class.getSimpleName());
        if (mCommonView == null) {
            mCommonView = new HeimaAsyncTask(UiUtil.getContext()) {
                @Override
                protected void initView() {
                    super.initView();
                    onPreExecute();
                }

                @Override
                public Result getServerData() {
                    mCategoryFragmentPresenter = new CategoryFragmentPresenter(CategoryBaseFragment.this);
                    mCategoryFragmentPresenter.loadCategoryInfo();
                    return doInBackground();
                }

                @Override
                protected View getSuccessView() {
                    return onPostExcute();
                }
            };
        }
//            mCommonView.getDataFromServer();
        return mCommonView.getCurrentView();
    }

    protected void onPreExecute() {
    }

    protected abstract View onPostExcute();

    public abstract HeimaAsyncTask.Result doInBackground();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d(TAG, "onActivityCreated: ");
        super.onActivityCreated(savedInstanceState);

//        mCommonView.getDataFromServer();
    }

    public void loadData() {
        if(mCommonView != null) {
            mCommonView.getDataFromServer();
        }
    }

    /**服务器代码出错*/
    public void onCategoryServerBug() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), "服务器error", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**网络问题*/
    public void onCategoryError() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), "网络error", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**一级标题*/
    protected List<CategoryVo.CategoryBean> mTitlesBean = new ArrayList<>();
    /**一级标题与二级标题*/
    protected Map<String, List<CategoryVo.CategoryBean>> mMap = new HashMap<>();
    /**二级标题与三级标题*/
    protected Map<CategoryVo.CategoryBean, List<CategoryVo.CategoryBean>> mMap2 = new HashMap<>();

    public void onCategorySuccess(List<CategoryVo.CategoryBean> categoryItems) {
        for (int i = 0; i < categoryItems.size(); i++) {
            CategoryVo.CategoryBean categoryBean = categoryItems.get(i);
            if(categoryBean.getParentId() == 0) {
                mTitlesBean.add(categoryBean);
            }
        }

        for (int i = 0; i < mTitlesBean.size(); i++) {
            List<CategoryVo.CategoryBean> mList = new ArrayList<>();
            CategoryVo.CategoryBean titleBean = mTitlesBean.get(i);
            for (int j = 0; j < categoryItems.size(); j++) {
                CategoryVo.CategoryBean categoryBean = categoryItems.get(j);
                if(titleBean.getId() == categoryBean.getParentId()) {
                    mList.add(categoryBean);
                }
            }
            mMap.put(titleBean.getName(), mList);
        }

        for (int i = 0; i < mTitlesBean.size(); i++) {

            String name = mTitlesBean.get(i).getName();
//            Log.d("CategoryFragment", "一级标题：" + name);
            List<CategoryVo.CategoryBean> categoryBeenList = mMap.get(name);
            for (int j = 0; j < categoryBeenList.size(); j++) {
                CategoryVo.CategoryBean bean1 = categoryBeenList.get(j);

//                Log.d("CategoryFragment", "二级标题：" + bean1.getName());
                List<CategoryVo.CategoryBean> mList = new ArrayList<>();
                for (int k = 0; k < categoryItems.size(); k++) {
                    CategoryVo.CategoryBean categoryBean = categoryItems.get(k);
                    if(bean1.getId() == categoryBean.getParentId()) {
                        mList.add(categoryBean);
//                        Log.d("CategoryFragment", "三级标题：" + categoryBean.getName());
                    }
                }
                mMap2.put(bean1, mList);
            }
        }

        Log.d(TAG, "mTitlesBean.size():" + "mTitlesBean.size():" + mTitlesBean.size());
        Log.d(TAG, "mTitlesBean.size():" + "mMap.size():" + mMap.size());
        Log.d(TAG, "mTitlesBean.size():" + "mMap2.size():" + mMap2.size());
    }

    protected List<CategoryInfo> transfore(CategoryVo.CategoryBean bean, List<CategoryVo.CategoryBean> categoryBeen) {
        List<CategoryInfo> infoList = new ArrayList<>();
        //标题title的转换
        CategoryInfo titleInfo = new CategoryInfo();
        titleInfo.type = CategoryInfo.TYPE_TITLE;
        titleInfo.title = bean.getName();
        infoList.add(titleInfo);

        //子bean的转换
        if(categoryBeen.size() % 3 == 0) {
            int count = 0;
            for (int i = 0; i < categoryBeen.size() / 3; i++) {
                //三级标题3个，6个
                CategoryInfo info = new CategoryInfo();

                info.type = CategoryInfo.TYPE_NORMAL;
                info.name1 = categoryBeen.get(count * 3 + 0).getName();
                info.name2 = categoryBeen.get(count * 3 + 1).getName();
                info.name3 = categoryBeen.get(count * 3 + 2).getName();

                info.url1 = categoryBeen.get(count * 3 + 0).getPic();
                info.url2 = categoryBeen.get(count * 3 + 1).getPic();
                info.url3 = categoryBeen.get(count * 3 + 2).getPic();

                info.id1 = categoryBeen.get(count * 3 + 0).getId();
                info.id2 = categoryBeen.get(count * 3 + 1).getId();
                info.id3 = categoryBeen.get(count * 3 + 2).getId();

                count++;

                infoList.add(info);
            }
        }else {
            int count = 0;
            for (int i = 0; i < categoryBeen.size() / 3 + 1; i++) {
                //三级标题4个，5个
                CategoryInfo info = new CategoryInfo();

                info.type = CategoryInfo.TYPE_NORMAL;

                //如果是最后一组
                if(i == categoryBeen.size() / 3) {
                    int sum = categoryBeen.size() % 3;
                    info.name1 = categoryBeen.get(count * 3 + 0).getName();
                    info.url1 = categoryBeen.get(count * 3 + 0).getPic();
                    info.id1 = categoryBeen.get(count * 3 + 0).getId();
                    if(sum != 1) {
                        info.name2 = categoryBeen.get(count * 3 + 1).getName();
                        info.url2 = categoryBeen.get(count * 3 + 1).getPic();
                        info.id2 = categoryBeen.get(count * 3 + 1).getId();
                    }
                }else {
                    info.name1 = categoryBeen.get(count * 3 + 0).getName();
                    info.name2 = categoryBeen.get(count * 3 + 1).getName();
                    info.name3 = categoryBeen.get(count * 3 + 2).getName();

                    info.url1 = categoryBeen.get(count * 3 + 0).getPic();
                    info.url2 = categoryBeen.get(count * 3 + 1).getPic();
                    info.url3 = categoryBeen.get(count * 3 + 2).getPic();

                    info.id1 = categoryBeen.get(count * 3 + 0).getId();
                    info.id2 = categoryBeen.get(count * 3 + 1).getId();
                    info.id3 = categoryBeen.get(count * 3 + 2).getId();
                }
                count++;
                infoList.add(info);
            }
        }

        return infoList;
    }
}
